//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "LiborForwardModelProcess.h"
using namespace Cephei::QL::Legacy::Libormarketmodels;
#include <gen/QL/Math/Array.h>
#include <gen/QL/CashFlow.h>
#include <gen/QL/Legacy/Libormarketmodels/LfmCovarianceParameterization.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/StochasticProcess.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
using namespace Cephei::QL::Indexes;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::CLiborForwardModelProcess (UInt64 size, Cephei::QL::Indexes::IIborIndex^ index) : CStochasticProcess(CLiborForwardModelProcess::typeid)
{
    CIborIndex^ _Cindex;
    try
    {
#ifdef HANDLE
        _phLiborForwardModelProcess = NULL;
#endif
        QuantLib::Size _size = (QuantLib::Size)ValueHelper::Convert (size);
        _Cindex = safe_cast<CIborIndex^> (index);
        _Cindex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _index = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_Cindex->GetShared ()); 
        _ppLiborForwardModelProcess = new boost::shared_ptr<QuantLib::LiborForwardModelProcess> (new QuantLib::LiborForwardModelProcess ( _size,  _index ));
        SetStochasticProcess (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppLiborForwardModelProcess));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cindex != nullptr) _Cindex->Unlock();
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::CLiborForwardModelProcess (boost::shared_ptr<QuantLib::LiborForwardModelProcess>& childNative, Object^ owner) : CStochasticProcess(CLiborForwardModelProcess::typeid)
{
#ifdef HANDLE
	_phLiborForwardModelProcess = NULL;
#endif
	_ppLiborForwardModelProcess = &childNative;
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppLiborForwardModelProcess));
}
Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::CLiborForwardModelProcess (QuantLib::LiborForwardModelProcess& childNative, Object^ owner) : CStochasticProcess(CLiborForwardModelProcess::typeid)
{
#ifdef HANDLE
	_phLiborForwardModelProcess = NULL;
#endif
	_ppLiborForwardModelProcess = new boost::shared_ptr<QuantLib::LiborForwardModelProcess> (&childNative);
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppLiborForwardModelProcess));
    _LiborForwardModelProcessOwner = owner;
    _StochasticProcessOwner = owner;
}

Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::CLiborForwardModelProcess (CLiborForwardModelProcess^ copy) : CStochasticProcess(CLiborForwardModelProcess::typeid)
{
#ifdef HANDLE
	_phLiborForwardModelProcess = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLiborForwardModelProcess = new boost::shared_ptr<QuantLib::LiborForwardModelProcess> (copy->GetShared());
        _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppLiborForwardModelProcess));
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::CLiborForwardModelProcess (System::Type^ t) : CStochasticProcess(CLiborForwardModelProcess::typeid)
{
#ifdef HANDLE
	_phLiborForwardModelProcess = NULL;
#endif
	if (!t->IsSubclassOf(CLiborForwardModelProcess::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::CLiborForwardModelProcess (QuantLib::Handle<QuantLib::LiborForwardModelProcess>& childNative, Object^ owner)  : CStochasticProcess(CLiborForwardModelProcess::typeid)
{
	_phLiborForwardModelProcess = &childNative;
	_ppLiborForwardModelProcess = &static_cast<boost::shared_ptr<QuantLib::LiborForwardModelProcess>>(childNative.currentLink());
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppLiborForwardModelProcess));
    _LiborForwardModelProcessOwner = owner;
}
Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::CLiborForwardModelProcess (QuantLib::Handle<QuantLib::LiborForwardModelProcess> childNative)  : CStochasticProcess(CLiborForwardModelProcess::typeid)
{
	_phLiborForwardModelProcess = &childNative;
	_ppLiborForwardModelProcess = &static_cast<boost::shared_ptr<QuantLib::LiborForwardModelProcess>>(childNative.currentLink());
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppLiborForwardModelProcess));
}
#endif
#ifdef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::CLiborForwardModelProcess (QuantLib::LiborForwardModelProcess childNative)  : CStochasticProcess(CLiborForwardModelProcess::typeid)
{
#ifdef HANDLE
	_phLiborForwardModelProcess = NULL;
#endif
	_ppLiborForwardModelProcess = new boost::shared_ptr<QuantLib::LiborForwardModelProcess> (new QuantLib::LiborForwardModelProcess (childNative));
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppLiborForwardModelProcess));
}
#endif

Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::~CLiborForwardModelProcess ()
{
    if (_ppLiborForwardModelProcess != NULL)
    {
	    delete _ppLiborForwardModelProcess;
        _ppLiborForwardModelProcess = NULL;
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::!CLiborForwardModelProcess ()
{
    if (_ppLiborForwardModelProcess != NULL)
    {
	    delete _ppLiborForwardModelProcess;
    }
}
QuantLib::LiborForwardModelProcess& Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::GetReference ()
{
    if (_ppLiborForwardModelProcess == NULL) throw gcnew NativeNullException ();
	return **_ppLiborForwardModelProcess;
}
boost::shared_ptr<QuantLib::LiborForwardModelProcess>& Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::GetShared ()
{
    if (_ppLiborForwardModelProcess == NULL) throw gcnew NativeNullException ();
	return *_ppLiborForwardModelProcess;
}
QuantLib::LiborForwardModelProcess* Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::GetPointer ()
{
    if (_ppLiborForwardModelProcess == NULL) throw gcnew NativeNullException ();
	return &**_ppLiborForwardModelProcess;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LiborForwardModelProcess>& Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::GetHandle ()
{
	if (_phLiborForwardModelProcess == NULL)
	{
		_phLiborForwardModelProcess = new Handle<QuantLib::LiborForwardModelProcess> (*_ppLiborForwardModelProcess);
	}
	return *_phLiborForwardModelProcess;
}
#endif
bool Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::HasNative () 
{
	return (_ppLiborForwardModelProcess != NULL);
}

Cephei::IVector<Double>^ Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::AccrualEndTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> & _rv = (std::vector<QuantLib::Time> &)(*_ppLiborForwardModelProcess)->accrualEndTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::AccrualStartTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> & _rv = (std::vector<QuantLib::Time> &)(*_ppLiborForwardModelProcess)->accrualStartTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Cephei::QL::ICashFlow^>^ Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::CashFlows (Microsoft::FSharp::Core::FSharpOption<Double>^ amount)
{
    try
    {
        QuantLib::Real _amount = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (amount) ? (QuantLib::Real)ValueHelper::Convert (amount->Value) : 1.0); //9a
    	std::vector<boost::shared_ptr<QuantLib::CashFlow> > _rv = (std::vector<boost::shared_ptr<QuantLib::CashFlow> >)(*_ppLiborForwardModelProcess)->cashFlows ( _amount );   
        CoVector<Cephei::QL::ICashFlow^>^ _nrv = gcnew CoVector<Cephei::QL::ICashFlow^>(gcnew CCashFlowVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Legacy::Libormarketmodels::ILfmCovarianceParameterization^ Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::CovarParam::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::LfmCovarianceParameterization> _rv = (boost::shared_ptr<QuantLib::LfmCovarianceParameterization>)(*_ppLiborForwardModelProcess)->covarParam ( );   
        Cephei::QL::Legacy::Libormarketmodels::CLfmCovarianceParameterization^ _nrv = gcnew Cephei::QL::Legacy::Libormarketmodels::CLfmCovarianceParameterization (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::DiscountBond (Cephei::IVector<Double>^ rates)
{
    CoVector<Double>^ _Crates;
    try
    {
        _Crates = safe_cast<CoVector<Double>^> (rates);
        _Crates->Lock();
        INativeVector<Double>^ _NCIrates = _Crates->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCrates = safe_cast<CDoubleVector^>(_NCIrates);
        std::vector<QuantLib::Rate>& _rates = static_cast<std::vector<QuantLib::Rate>&> (_NCrates->GetReference ());
    	std::vector<QuantLib::DiscountFactor> _rv = (std::vector<QuantLib::DiscountFactor>)(*_ppLiborForwardModelProcess)->discountBond ( _rates );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Crates != nullptr) _Crates->Unlock();
    }
}
UInt64 Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::Factors::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppLiborForwardModelProcess)->factors ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<DateTime>^ Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::FixingDates::get ()
{
    try
    {
    	std::vector<QuantLib::Date> & _rv = (std::vector<QuantLib::Date> &)(*_ppLiborForwardModelProcess)->fixingDates ( );   
        Cephei::IVector<DateTime>^ _nrv = gcnew CoVector<DateTime> (gcnew CDateTimeVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::FixingTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> & _rv = (std::vector<QuantLib::Time> &)(*_ppLiborForwardModelProcess)->fixingTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Indexes::IIborIndex^ Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::Index::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::IborIndex> _rv = (boost::shared_ptr<QuantLib::IborIndex>)(*_ppLiborForwardModelProcess)->index ( );   
        Cephei::QL::Indexes::CIborIndex^ _nrv = gcnew Cephei::QL::Indexes::CIborIndex (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::NextIndexReset (Double t)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppLiborForwardModelProcess)->nextIndexReset ( _t );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Legacy::Libormarketmodels::ILiborForwardModelProcess^ Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::SetCovarParam (Cephei::QL::Legacy::Libormarketmodels::ILfmCovarianceParameterization^ param)
{
    CLfmCovarianceParameterization^ _Cparam;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cparam = safe_cast<CLfmCovarianceParameterization^> (param);
        _Cparam->Lock();
        boost::shared_ptr<QuantLib::LfmCovarianceParameterization>& _param = static_cast<boost::shared_ptr<QuantLib::LfmCovarianceParameterization>&> (_Cparam->GetShared ()); 
    	(*_ppLiborForwardModelProcess)->setCovarParam ( _param );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cparam != nullptr) _Cparam->Unlock();
    }
}
UInt64 Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppLiborForwardModelProcess)->size ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Legacy::Libormarketmodels::ILiborForwardModelProcess^ Cephei::QL::Legacy::Libormarketmodels::CLiborForwardModelProcess_Factory::Create (UInt64 size, Cephei::QL::Indexes::IIborIndex^ index)
{
    return gcnew CLiborForwardModelProcess ( size,  index);
}
